﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using KingRoad.EPM.PersonnelLogEntity.Model;
using Unis.Framework.Core.Utility;
using KingRoad.EPM.PersonnelLogDAL.ScmSafetyassessmentService;
using KingRoad.EPM.PersonnelLogDAL.ProjectUnitService;
using KingRoad.EPM.PersonnelLogDAL.ScmAssessmentunitService;
using KingRoad.EPM.PersonnelLogDAL.ScmAssessmentscoreService;
using KingRoad.EPM.PersonnelLogDAL.ScmAssessmentitemsService;


namespace KingRoad.EPM.PersonnelLogBLL.UIRules
{
    /// <summary> 
    /// 客户端业务处理类。 
    /// 文件生成时间 2017/1/12 14:00:02
    /// </summary> 
    public class ScmSafetyassessmentUIRule
    {
        private readonly IScmSafetyassessmentService _scmSafetyassessmentService;
        private ProjectUnitUIRule _projectunitUIRule;
        private ScmAssessmentunitUIRule _scmassessmentunitUIRule;
        private ScmAssessmentitemsUIRule _scmassessmentitemsUIRule;
        private ScmAssessmentscoreUIRule _scmassessmentscoreUIRule;

        public ScmSafetyassessmentUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public ScmSafetyassessmentUIRule(IScmSafetyassessmentService scmSafetyassessmentService)
        {
            _scmSafetyassessmentService = scmSafetyassessmentService;
        }

        /// <summary>
        /// 获取所有的安全考核信息
        /// </summary>
        /// <returns></returns>
        public List<ScmSafetyassessmentInfo> GetAllSafetyassessment()
        {
            var list = _scmSafetyassessmentService.GetEntityList();
            return list;
        }
        /// <summary>
        /// 根据条件查询安全知识信息
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <param name="sqlCondition">sql查询条件</param>
        /// <returns></returns>
        public List<ScmSafetyassessmentInfo> GetAllSafetyassessmentForPage(int numCurrentPage, int numPerPage,
                                                                        string orderByField, out int totalRecord, string sqlCondition)
        {
            var list = _scmSafetyassessmentService.GetEntityListByContionRe(numCurrentPage, numPerPage, orderByField, out totalRecord, sqlCondition);
            return list;
        }
        /// <summary>
        /// 根据主键获取安全考核信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public ScmSafetyassessmentInfo GetSafetyassessmentInfo(Guid id)
        {
            return _scmSafetyassessmentService.GetEntityById(id);
        }
        /// <summary>
        /// 分页获取所有的安全考核信息
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <returns></returns>
        public List<ScmSafetyassessmentInfo> GetAllSafetyassessment(int numCurrentPage, int numPerPage, string orderByField, out int totalRecord)
        {
            var list = _scmSafetyassessmentService.GetEntityListByContion(numCurrentPage, numPerPage, orderByField, out totalRecord);
            return list;
        }
        /// <summary>
        /// 添加安全考核信息
        /// </summary>
        /// <param name="info">安全考核信息</param>
        /// <param name="ucodes">被考核单位合同号数组</param>
        /// <param name="del_items">被排除的考核项目Ids</param>
        /// <returns></returns>
        public void AddSafetyassessment(ScmSafetyassessmentInfo info, string ucodes, string del_items)
        {
            //添加考核信息
            _scmSafetyassessmentService.InsertEntity(info);
            //金途-监理/施工单位BLL
            _projectunitUIRule = new ProjectUnitUIRule(new ProjectUnitService());
            string type = info.Scd_ID;
            if (type == "1D6426E7-08CB-48F2-B11A-834389148D7C")
                type = "0";
            else
                type = "1";
            //TODO:获取单位信息
            //type=0时 unitList存放 根据ucodes获取监理单位信息（总监+驻地+检查）
            //type=1时 unitList存放 根据ucodes获取施工单位信息
            List<ProjectUnitInfo> unitList = _projectunitUIRule.GetUnitByUnitCodes(ucodes);

            //被考核单位BLL
            _scmassessmentunitUIRule = new ScmAssessmentunitUIRule(new ScmAssessmentunitService());
            //考核项目信息BLL
            _scmassessmentitemsUIRule = new ScmAssessmentitemsUIRule(new ScmAssessmentitemsService());
            var itemsList = _scmassessmentitemsUIRule.GetAssessmentitemsByType(info.Scd_ID);
            //安全考核项目设置及打分信息BLL
            _scmassessmentscoreUIRule = new ScmAssessmentscoreUIRule(new ScmAssessmentscoreService());
            foreach (ProjectUnitInfo uinfo in unitList)
            {
                //添加被考核单位信息
                ScmAssessmentunitInfo auinfo = new ScmAssessmentunitInfo();
                auinfo.Au_ID = Guid.NewGuid().ToString();
                auinfo.Sa_ID = info.Sa_ID;
                auinfo.Au_UnitID = uinfo.UNIT_ID;
                auinfo.Au_Section = uinfo.UNIT_CODE;
                auinfo.Au_UnitName = uinfo.UNIT_NAME;
                _scmassessmentunitUIRule.AddAssessmentunit(auinfo);
                //被考核项目
                foreach (ScmAssessmentitemsInfo aiinfo in itemsList)
                {
                    //添加考核项目设置及打分信息
                    ScmAssessmentscoreInfo asinfo = new ScmAssessmentscoreInfo();
                    asinfo.Sa_ID = info.Sa_ID;
                    asinfo.Ai_ID = aiinfo.Ai_ID;
                    asinfo.Au_ID = auinfo.Au_ID;
                    if (del_items.IndexOf(asinfo.Ai_ID) >= 0)
                    {
                        asinfo.As_Include = false;
                        asinfo.As_UnitInclude = false;
                    }
                    else
                    {
                        asinfo.As_Include = true;
                        asinfo.As_UnitInclude = true;
                    }
                    _scmassessmentscoreUIRule.AddAssessmentscore(asinfo);
                }
            }
        }
        /// <summary>
        /// 修改安全考核信息
        /// </summary>
        /// <param name="info">安全考核信息</param>
        /// <param name="ucodes">被考核单位合同号数组</param>
        /// <param name="del_items">被排除的考核项目Ids</param>
        /// <returns></returns>
        public void EditSafetyassessment(ScmSafetyassessmentInfo info, string ucodes, string del_items)
        {
            //修改考核信息
            _scmSafetyassessmentService.UpdateEntity(info);
            //安全考核项目设置及打分信息BLL
            _scmassessmentscoreUIRule = new ScmAssessmentscoreUIRule(new ScmAssessmentscoreService());
            //修改前清除项目打分信息
            _scmassessmentscoreUIRule.DeleteAssessmentscoreBySaID(info.Sa_ID.ToString());
            //被考核单位BLL
            _scmassessmentunitUIRule = new ScmAssessmentunitUIRule(new ScmAssessmentunitService());
            //修改前清除被考核单位信息
            _scmassessmentunitUIRule.DeleteAssessmentunitBySaID(info.Sa_ID.ToString());
            //金途-监理/施工单位BLL
            _projectunitUIRule = new ProjectUnitUIRule(new ProjectUnitService());
            string type = info.Scd_ID;
            if (type == "1D6426E7-08CB-48F2-B11A-834389148D7C")
                type = "0";
            else
                type = "1";
            //TODO:获取单位信息
            //type=0时 unitList存放 根据ucodes获取监理单位信息（总监+驻地+检查）
            //type=1时 unitList存放 根据ucodes获取施工单位信息
            List<ProjectUnitInfo> unitList = _projectunitUIRule.GetUnitByUnitCodes(ucodes);
            //考核项目信息BLL
            _scmassessmentitemsUIRule = new ScmAssessmentitemsUIRule(new ScmAssessmentitemsService());
            var itemsList = _scmassessmentitemsUIRule.GetAssessmentitemsByType(info.Scd_ID);

            foreach (ProjectUnitInfo uinfo in unitList)
            {
                //添加被考核单位信息
                ScmAssessmentunitInfo auinfo = new ScmAssessmentunitInfo();
                auinfo.Au_ID = Guid.NewGuid().ToString();
                auinfo.Sa_ID = info.Sa_ID;
                auinfo.Au_UnitID = uinfo.UNIT_ID;
                auinfo.Au_Section = uinfo.UNIT_CODE;
                auinfo.Au_UnitName = uinfo.UNIT_NAME;
                _scmassessmentunitUIRule.AddAssessmentunit(auinfo);
                //被考核项目
                foreach (ScmAssessmentitemsInfo aiinfo in itemsList)
                {
                    //添加考核项目设置及打分信息
                    ScmAssessmentscoreInfo asinfo = new ScmAssessmentscoreInfo();
                    asinfo.Sa_ID = info.Sa_ID;
                    asinfo.Ai_ID = aiinfo.Ai_ID;
                    asinfo.Au_ID = auinfo.Au_ID;
                    if (del_items.IndexOf(asinfo.Ai_ID) >= 0)
                    {
                        asinfo.As_Include = false;
                        asinfo.As_UnitInclude = false;
                    }
                    else
                    {
                        asinfo.As_Include = true;
                        asinfo.As_UnitInclude = true;
                    }
                    _scmassessmentscoreUIRule.AddAssessmentscore(asinfo);
                }
            }
        }
        /// <summary>
        /// 根据主键删除安全考核信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public void DeleteSafetyassessmentById(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                //删除得分信息
                _scmassessmentscoreUIRule = new ScmAssessmentscoreUIRule(new ScmAssessmentscoreService());
                _scmassessmentscoreUIRule.DeleteAssessmentscoreBySaID(id.ToString());
                //删除被考核单位信息
                _scmassessmentunitUIRule = new ScmAssessmentunitUIRule(new ScmAssessmentunitService());
                _scmassessmentunitUIRule.DeleteAssessmentunitBySaID(id.ToString());
                //删除考核信息
                _scmSafetyassessmentService.DeleteEntityByStringID(id);
            }
        }

        /// <summary>
        /// 上报安全考核得分信息
        /// </summary>
        /// <param name="info">安全考核信息</param>
        /// <param name="ulist">被考核单位得分情况</param>
        /// <returns></returns>
        public void ReportSafetyassessment(ScmSafetyassessmentInfo info, List<ScmAssessmentunitInfo> ulist)
        {
            //修改考核信息
            _scmSafetyassessmentService.UpdateEntity(info);
            //修改被考核单位得分信息
            _scmassessmentunitUIRule = new ScmAssessmentunitUIRule(new ScmAssessmentunitService());
            foreach (var uinfo in ulist)
            {
                _scmassessmentunitUIRule.EditAssessmentunit(uinfo);
            }
        }
        /// <summary>
        /// 上报安全考核分项得分信息
        /// </summary>
        /// <param name="info">安全考核信息</param>
        /// <param name="slist">考核分项得分信息</param>
        /// <returns></returns>
        public void ReportSafetyassessmentitem(ScmSafetyassessmentInfo info, List<ScmAssessmentscoreInfo> slist)
        {
            //修改考核信息
            _scmSafetyassessmentService.UpdateEntity(info);
            //修改考核分项得分信息
            _scmassessmentscoreUIRule = new ScmAssessmentscoreUIRule(new ScmAssessmentscoreService());
            //得分
            decimal? score = 0;
            //总分
            decimal? allscore = 0;
            foreach (var sinfo in slist)
            {
                if (sinfo.As_Score != null)
                {
                    score += sinfo.As_Score;
                    allscore += sinfo.Ai_AllScore;
                }
                _scmassessmentscoreUIRule.EditAssessmentscore(sinfo);
            }
            if (allscore != 0)
            { //修改被考核单位得分信息
                _scmassessmentunitUIRule = new ScmAssessmentunitUIRule(new ScmAssessmentunitService());
                var uinfo = _scmassessmentunitUIRule.GetAssessmentunitInfo(new Guid(slist[0].Au_ID));
                uinfo.Au_Score = (score / allscore) * 100;
                _scmassessmentunitUIRule.EditAssessmentunit(uinfo);
            }
        }
    }
}
